<?php

/**
 * news actions.
 *
 * @package    Moncada
 * @subpackage news
 * @author     Federico Motta - Andrea Burza
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class newsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	  	$culture=$this->getUser()->getCulture();
	    $this->news = Doctrine_Core::getTable('Messaggi')->getSelected(2,$culture);
	    $this->pager = new sfDoctrinePager(
							'Articoli',
							10
							);

		$this->pager->setPage($request->getParameter('page', 1));
		$this->pager->init();
		$this->articolis = $this->pager->getResults();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->articoli = Doctrine_Core::getTable('Articoli')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->articoli);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ArticoliForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ArticoliForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($articoli = Doctrine_Core::getTable('Articoli')->find(array($request->getParameter('id'))), sprintf('Object articoli does not exist (%s).', $request->getParameter('id')));
    $this->form = new ArticoliForm($articoli);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($articoli = Doctrine_Core::getTable('Articoli')->find(array($request->getParameter('id'))), sprintf('Object articoli does not exist (%s).', $request->getParameter('id')));
    $this->form = new ArticoliForm($articoli);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($articoli = Doctrine_Core::getTable('Articoli')->find(array($request->getParameter('id'))), sprintf('Object articoli does not exist (%s).', $request->getParameter('id')));
    $articoli->delete();

    $this->redirect('news/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $articoli = $form->save();

      $this->redirect('news/edit?id='.$articoli->getId());
    }
  }
}
